﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Waiter.Common;
using Waiter.Controler;
using Waiter.DataLayer;
using DevComponents.DotNetBar.Controls;
using DevComponents.DotNetBar;

namespace Waiter.View
{
    public partial class frmWorkShifts : DevComponents.DotNetBar.Office2007Form//Form
    {
        WorkShiftCtrl myWorkControl = new WorkShiftCtrl();
        StaffCtrl myStaffControl = new StaffCtrl();
        public delegate void CloseForm();                       //Delegate close form
        public CloseForm myCloseForm;                           //Tạo thể hiện của delegate

        #region Functions ****************************************************

        public frmWorkShifts()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Load data init vào các control
        /// </summary>
        private void fsLoadGrid()
        {
            CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_INFO, "frmWorkShifts.fsLoadGrid", "", "");
            //Load danh sách nhân viên vào ComboBox.
            myStaffControl.fsLoadComboBox_ListStaff(cmbStaffName);

            //Load trạng thái làm việc vào ComboBox
            myStaffControl.fsLoadComboBox_WorkStatus(cmbWorkStatus);

            //Load data mặc định vào Grid
            this.grdMain.DataSource = myWorkControl.fsGetFullListWorkShift();
        }

        #endregion

        #region Events *************************************************

        private void frmWorkShifts_Load(object sender, EventArgs e)
        {
            CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_INFO, "frmWorkShifts.frmWorkShifts_Load", "", "");
            this.fsLoadGrid();
        }

        /// <summary>
        /// Thay đổi trạng thái chọn/không chọn chkStaffName
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void chkStaffName_CheckedChanged(object sender, System.EventArgs e)
        {
            CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_INFO, "frmWorkShifts.chkStaffName_CheckedChanged", "", "");
            if (chkStaffName.Checked == true)
            {
                cmbStaffName.Enabled = true;
            }
            else
            {
                cmbStaffName.Enabled = false;
            }
        }

        /// <summary>
        /// Thay đổi trạng thái chọn/không chọn chkWorkStatus
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void chkWorkStatus_CheckedChanged(object sender, System.EventArgs e)
        {
            CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_INFO, "frmWorkShifts.chkWorkStatus_CheckedChanged", "", "");
            if (chkWorkShift.Checked == true)
            {
                cmbWorkStatus.Enabled = true;
            }
            else
            {
                cmbWorkStatus.Enabled = false;
            }
        }

        /// <summary>
        /// Click button search
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSearch_Click(object sender, System.EventArgs e)
        {
            CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_INFO, "frmWorkShifts.btnSearch_Click", "", "");
            if (chkWorkShift.Checked == true && chkStaffName.Checked == true)
            {
                grdMain.DataSource = myWorkControl.fsGetListWorkShift(cmbStaffName.SelectedValue.ToString(), cmbWorkStatus.SelectedValue.ToString());
            }
            else if (chkWorkShift.Checked == true)
            {
                grdMain.DataSource = myWorkControl.fsGetListWorkShift(string.Empty, cmbWorkStatus.SelectedValue.ToString());
            }
            else if (chkStaffName.Checked == true)
            {
                grdMain.DataSource = myWorkControl.fsGetListWorkShift(cmbStaffName.SelectedValue.ToString(), string.Empty);
            }
            else
            {
                grdMain.DataSource = myWorkControl.fsGetFullListWorkShift();
            }
        }

        /// <summary>
        /// Click button save
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSave_Click(object sender, System.EventArgs e)
        {
            CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_INFO, "frmWorkShifts.btnSave_Click", "", "");
            ResultStatus IsSaveSuscess = ResultStatus.SUCCESS;
            List<VIEW_WorkShiftsByColumn> myList = (List<VIEW_WorkShiftsByColumn>)grdMain.DataSource;

            foreach (var row in myList)
            {
                //Save ca 1
                if (IsSaveSuscess == ResultStatus.SUCCESS)
                    IsSaveSuscess = myWorkControl.fsUpdateWorkShift(row.strSTAFF_CODE, row.strWORK_SHIFTS_CODE_1, row.bIS_WORK_1);
                else
                    break;
                //Neu save ca 1 thanh cong thi save ca 2
                if (IsSaveSuscess == ResultStatus.SUCCESS)
                    IsSaveSuscess = myWorkControl.fsUpdateWorkShift(row.strSTAFF_CODE, row.strWORK_SHIFTS_CODE_2, row.bIS_WORK_2);
                else
                    break;
                //Neu save ca 2 thanh cong thi save ca 3
                if (IsSaveSuscess == ResultStatus.SUCCESS)
                    IsSaveSuscess = myWorkControl.fsUpdateWorkShift(row.strSTAFF_CODE, row.strWORK_SHIFTS_CODE_3, row.bIS_WORK_3);
                else
                    break;
                //Neu save ca 3 thanh cong thi save ca 4
                if (IsSaveSuscess == ResultStatus.SUCCESS)
                    IsSaveSuscess = myWorkControl.fsUpdateWorkShift(row.strSTAFF_CODE, row.strWORK_SHIFTS_CODE_4, row.bIS_WORK_4);
                else
                    break;
            }

            if (IsSaveSuscess == ResultStatus.SUCCESS)
            {
                MessageBoxEx.Show("Đã lưu thành công", CommonMessages.MSG_NOTICE_TITLE, MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                MessageBoxEx.Show(CommonMessages.MSG_SAVE_FAIL, CommonMessages.MSG_ERR_TITLE, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        /// <summary>
        /// Click button back
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnBack_Click(object sender, System.EventArgs e)
        {
            CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_INFO, "frmWorkShifts.btnBack_Click", "", "");
            this.Close();
        }

        /// <summary>
        /// Thay đổi tiêu đề của form
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void headerItem1_Load(object sender, EventArgs e)
        {
            CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_INFO, "frmWorkShifts.headerItem1_Load", "", "");
            this.headerItem1.init("CA LÀM VIỆC", false);
            //this.headerItem1.btnChangeStyle.Visible = false;
            //this.headerItem1.btnSwitchUser.Visible = false;
            //this.headerItem1.lblShift.Visible = false;
            //this.headerItem1.lblShift1.Visible = false;
            ////this.headerItem1.lblName.Text = Waiter.Properties.Settings.Default.STAFF_LOGIN_NAME;
        }

        /// <summary>
        /// Khi form đóng lại
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void frmWorkShifts_FormClosing(object sender, FormClosingEventArgs e)
        {
            CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_INFO, "frmWorkShifts.frmWorkShifts_FormClosing", "", "");
            if (myCloseForm != null)//Thông báo cho form menu
                myCloseForm();
        }

        #endregion
    }
}
